/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tt.Servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tt.VorlesungenService.IcsCalendar;

import tt.model.*;
import tt.VorlesungenService.EntityService;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletOutputStream;


/**
 *
 * @author andi
 */
@WebServlet(name = "IcsCalendarServlet", urlPatterns = {"/IcsCalendarServlet"})
public class IcsCalendarServlet extends HttpServlet {

    @EJB
    private EntityService service;
    

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=calendar.ics");

        String id = request.getParameter("schedule");
        
        Logger.getLogger(IcsCalendarServlet.class.getName()).log(Level.INFO, id);
        
        try {
            Courseschedule mySchedule = service.getCourseScheduleByIDCourseSchedule(id);
            IcsCalendar mycalendar = new IcsCalendar(service.getGeneralSettingsByIDGeneral(1));

            StringBuffer sb = new StringBuffer(mycalendar.createIcsFile(mySchedule));
            InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
            ServletOutputStream out = response.getOutputStream();

            byte[] outputByte = new byte[4096];
            //copy binary contect to output stream
            while (in.read(outputByte, 0, 4096) != -1) {
                out.write(outputByte, 0, 4096);
            }//endwhile
            in.close();
            out.flush();
            out.close();

        } catch (Exception e) {
            Logger.getLogger(IcsCalendarServlet.class.getName()).log(Level.INFO, e.toString());
            
        }


//       response.setContentType("text/html;charset=UTF-8");
//       PrintWriter out = response.getWriter(); 
//    
//       try{
//            Courseschedule mySchedule = service.getCourseScheduleByIDCourseSchedule("1f8d77c2-81db-4bf4-bb53-004d149cfcc1"); 
//            IcsCalendar mycalendar = new IcsCalendar();
//            out.println(mycalendar.createCalendar(mySchedule));
//            
//       }catch(Exception e)
//       {
//           out.println(e);
//       }
//       
//       
//       out.println("Ich bin das ICS Kalender Servlet!");
//     
//       out.close(); 

    }//end method processRequest

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
